<?php
/**
 * Rights module tables
 * 
 * @package Database\Migrations
 */
class m130218_154240_rights extends CDbMigration {
 
    public function up() {
        $this->createTable("tbl_auth_item", array(
                "name" => "varchar(64)  NOT NULL",
                "type" => "integer NOT NULL",
                "description" => "text",
                "bizrule" => "text",
                "data" => "text",
                "primary key (name)"
            ));

        $this->createTable("tbl_auth_item_child", array(
                "parent" => "varchar(64)  NOT NULL",
                "child" => "varchar(64)  NOT NULL",
                "primary key (parent,child)",
            ));

        $this->createTable("tbl_rights", array(
                "itemname" => "varchar(64)  NOT NULL",
                "type" => "integer NOT NULL",
                "weight" => "integer NOT NULL",
                "primary key (itemname)",
            ));
            
        $this->createTable("tbl_auth_assignment", array(
                "itemname" => "varchar(64)  NOT NULL",
                "userid" => "varchar(64)  NOT NULL",
                "bizrule" => "text",
                "data" => "text",
                "primary key (itemname,userid)",
            ));
        
        $this->addForeignKey('fk_auth_assignment',  'tbl_auth_assignment', 'itemname', 'tbl_auth_item', 'name');
        $this->addForeignKey('fk_rights',           'tbl_rights',          'itemname', 'tbl_auth_item', 'name');
        $this->addForeignKey('fk_auth_item_parent', 'tbl_auth_item_child', 'parent',   'tbl_auth_item', 'name');
        $this->addForeignKey('fk_auth_item_child',  'tbl_auth_item_child', 'child',    'tbl_auth_item', 'name');
    }

    public function down() {
        $this->dropForeignKey('fk_auth_assignment');
        $this->dropForeignKey('fk_rights');
        $this->dropForeignKey('fk_auth_item_parent');
        $this->dropForeignKey('fk_auth_item_child');

        $this->dropTable('tbl_auth_assignment');
        $this->dropTable('tbl_auth_item');
        $this->dropTable('tbl_auth_item_child');
        $this->dropTable('tbl_rights');
    }
}